package com.menggudf.controller;

import com.menggudf.entity.AiMedicalRecord;
import com.menggudf.model.ResultModel;
import com.menggudf.model.UserModel;
import com.menggudf.model.request.AiMedicalRecordRequest;
import com.menggudf.service.AiMedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("aiMedicalRecord")
public class AiMedicalRecordController {

    @Resource
    AiMedicalRecordService aiMedicalRecordService;
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("insertAiMedicalRecord")
    @ResponseBody
    public ResultModel insertAiMedicalRecord(@RequestBody AiMedicalRecordRequest aiMedicalRecordRequest, HttpSession httpSession) {
        UserModel userModel = (UserModel)redisTemplate.opsForValue().get(httpSession.getId());
        if (userModel == null) {
            return ResultModel.fail("超时，请重新登录", null);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH:mm");
        AiMedicalRecord aiMedicalRecord = new AiMedicalRecord();
        aiMedicalRecord.setMedicalRecord(aiMedicalRecordRequest.getMedicalRecord());
        aiMedicalRecord.setCreateTime(new Date());
        aiMedicalRecord.setUserCode(userModel.getUserCode());
        aiMedicalRecord.setRecordName(userModel.getUserName());
        aiMedicalRecord.setRecordName(sdf.format(new Date()) + " 病历");
        ResultModel resultModel = aiMedicalRecordService.insertAiMedicalRecord(aiMedicalRecord);
        return resultModel;
    }

}
